package com.kobe.algorithm.leetcode.case3;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author ：caopengfei
 * @date ：2021/2/22
 */
public class Solution {

    public int lengthOfLongestSubstring(String s) {
            int n = s.length();
            int maxLength = 0;
            int right =-1;
            Set<Character> charSet = new HashSet<>();
            for(int i = 0; i < n; i++){
                if (i != 0) {
                    // 左指针向右移动一格，移除一个字符
                    charSet.remove(s.charAt(i-1));
                }

                while (right + 1 < n && !charSet.contains(s.charAt(right + 1))) {
                    // 不断地移动右指针
                    charSet.add(s.charAt(right+ 1));
                    ++right;
                }
                // 第 i 到 rk 个字符是一个极长的无重复字符子串
                maxLength = Math.max(maxLength, right - i + 1);
            }
            return maxLength;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.lengthOfLongestSubstring(" "));
    }

}
